package com.sidianzhong.sdz.service.impl;
import com.github.pagehelper.PageHelper;
import com.sidianzhong.sdz.mapper.*;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by hxgqh on 2016/1/7.
*/
@Service
@Transactional
public class UserChangeServiceImpl implements UserChangeService{
    protected static final Logger LOG = LoggerFactory.getLogger(UserChangeServiceImpl.class);
    // 将所有的modelMapper注入
        @Autowired
    private UserChangeMapper userChangeMapper;

    @Override
    public UserChange create(UserChange item) {
         userChangeMapper.insert(item);
         return item;
    }

    @Override
    public int delete(Integer id) {
        return userChangeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(UserChange item) {
        return userChangeMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public UserChange get(Integer id) {
        return userChangeMapper.selectByPrimaryKey(id);
    }
    @Override
    public PageInfo<UserChange> getListWithPaging(Integer pageNum, Integer pageSize,
                                                  String sortItem, String sortOrder,UserChange item){
       UserChangeExample  example = new UserChangeExample();
       UserChangeExample.Criteria criteria = example.createCriteria();
       if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getAddress() != null ){
            criteria.andAddressEqualTo(item.getAddress());
        }
        if(item.getAmount() != null ){
            criteria.andAmountEqualTo(item.getAmount());
        }
        if(item.getRemark() != null ){
            criteria.andRemarkEqualTo(item.getRemark());
        }
        if(item.getFee() != null ){
            criteria.andFeeEqualTo(item.getFee());
        }
        if(item.getGetAmount() != null ){
            criteria.andGetAmountEqualTo(item.getGetAmount());
        }
        if(item.getDrawHash() != null ){
            criteria.andDrawHashEqualTo(item.getDrawHash());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getType() != null ){
            criteria.andTypeEqualTo(item.getType());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<UserChange> list = this.userChangeMapper.selectByExample(example);
       PageInfo result = new PageInfo(list);
       return result;
    }

    @Override
    public List<UserChange> getList(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,UserChange item){
       UserChangeExample  example = new UserChangeExample();
       UserChangeExample.Criteria criteria = example.createCriteria();
       if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getAddress() != null ){
            criteria.andAddressEqualTo(item.getAddress());
        }
        if(item.getAmount() != null ){
            criteria.andAmountEqualTo(item.getAmount());
        }
        if(item.getRemark() != null ){
            criteria.andRemarkEqualTo(item.getRemark());
        }
        if(item.getFee() != null ){
            criteria.andFeeEqualTo(item.getFee());
        }
        if(item.getGetAmount() != null ){
            criteria.andGetAmountEqualTo(item.getGetAmount());
        }
        if(item.getDrawHash() != null ){
            criteria.andDrawHashEqualTo(item.getDrawHash());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getType() != null ){
            criteria.andTypeEqualTo(item.getType());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<UserChange> list = this.userChangeMapper.selectByExample(example);
       return list;
    }

     @Override
     public PageInfo<JSONObject> getListWithObject(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,UserChange item){
        UserChangeExample  example = new UserChangeExample();
        UserChangeExample.Criteria criteria = example.createCriteria();
        if(item.getUserId() != null ){
            criteria.andUserIdEqualTo(item.getUserId());
        }
        if(item.getAddress() != null ){
            criteria.andAddressEqualTo(item.getAddress());
        }
        if(item.getAmount() != null ){
            criteria.andAmountEqualTo(item.getAmount());
        }
        if(item.getRemark() != null ){
            criteria.andRemarkEqualTo(item.getRemark());
        }
        if(item.getFee() != null ){
            criteria.andFeeEqualTo(item.getFee());
        }
        if(item.getGetAmount() != null ){
            criteria.andGetAmountEqualTo(item.getGetAmount());
        }
        if(item.getDrawHash() != null ){
            criteria.andDrawHashEqualTo(item.getDrawHash());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getType() != null ){
            criteria.andTypeEqualTo(item.getType());
        }
        if(item.getCoinId() != null ){
            criteria.andCoinIdEqualTo(item.getCoinId());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
        example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List<UserChange> list = this.userChangeMapper.selectByExample(example);
        List<JSONObject> lists =  list.stream().map(userChange -> JSONObject.parseObject(JSONObject.toJSONString(userChange))).collect(Collectors.toList());
        PageInfo result = new PageInfo(lists);
        return result;
        }


    @Override
    public Double getAmount(Integer userId, Integer coinId, Integer status) {

        Double amount = this.userChangeMapper.getAmount(userId,coinId,status);
        return amount;
    }

    @Override
    public boolean isHashExist(String hash) {

        UserChangeExample  example = new UserChangeExample();
        UserChangeExample.Criteria criteria = example.createCriteria();
        criteria.andDrawHashEqualTo(hash);
        List< UserChange> list = this.userChangeMapper.selectByExample(example);
        if(list==null||list.size()<1) {
            return false;
        }else {

            return true;
        }
    }

    @Override
    public Double getDayAmount(Integer userId, Integer coinId) {
        Double amount = this.userChangeMapper.getDayAmount(userId,coinId);
        return amount;
    }

}
